/*
 * Copyright (C) 2010 TTPASI, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ttpasi.jgenere.chaocipher;

public class HoleFinder
{
  private final String _ct;
  private final String _pt;

  public HoleFinder(String ct, String pt)
  {
    this._ct = ct;
    this._pt = pt;
  }

  private boolean containsChar(char[] chars, int start, int end, char c)
  {
    for (int i = start; i <= end; i++)
    {
      if (chars[i] == c)
        return true;
    }
    return false;
  }

  public int countHoles()
  {
    int count = 0;
    char[] ct = _ct.toCharArray();
    char[] pt = _pt.toCharArray();
    for (int i = 1; i < ct.length; i++)
    {
      if (!containsChar(ct, 0, i - 1, ct[i]) && !containsChar(pt, 0, i - 1, pt[i]))
      {
        count++;
      }
    }
    return count;
  }

  public void logHoles()
  {
    System.out.print("holes at: ");
    char[] ct = _ct.toCharArray();
    char[] pt = _pt.toCharArray();
    for (int i = 1; i < ct.length; i++)
    {
      if (!containsChar(ct, 0, i - 1, ct[i]) && !containsChar(pt, 0, i - 1, pt[i]))
      {
        System.out.print(i + "[" + ct[i] + "," + pt[i] + "], ");
      }
    }
    System.out.println();
  }
  
  public int[] getHoles()
  {
    int[] holes = new int[countHoles()];
    char[] ct = _ct.toCharArray();
    char[] pt = _pt.toCharArray();
    for (int i = 1, count = 0; i < ct.length; i++)
    {
      if (!containsChar(ct, 0, i - 1, ct[i]) && !containsChar(pt, 0, i - 1, pt[i]))
      {
        holes[count] = i;
        count++;
      }
    }
    return holes;
  }
}
